

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/crystal575/img/favicon.png">
  <link rel="icon" href="/crystal575/img/favicon.png">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
    <meta http-equiv="Content-Security-Policy" content="upgrade-insecure-requests">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="Hello,here is crystal575&#39;s blog.">
  <meta name="author" content="crystal575">
  <meta name="keywords" content="">
  
  <title>golanggolang基础知识 - crystal575&#39;s blog</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/crystal575/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10.7.2/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
  



<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/crystal575/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"crystal575.gitee.io","root":"/crystal575/","version":"1.8.11","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/crystal575/js/utils.js" ></script>
  <script  src="/crystal575/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/crystal575/">&nbsp;<strong>crystal575's blog</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/crystal575/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/crystal575/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/crystal575/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/crystal575/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/crystal575/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/crystal575/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="golanggolang基础知识">
              
            </span>

            
              <div class="mt-3">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-author" aria-hidden="true"></i>
      crystal575
    </span>
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-11-13 17:13" pubdate>
        2021年11月13日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      2.2k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      40
       分钟
    </span>
  

  
  
    
      <!-- 不蒜子统计文章PV -->
      <span id="busuanzi_container_page_pv" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="busuanzi_value_page_pv"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">golanggolang基础知识</h1>
            
              <p class="note note-info">
                
                  本文最后更新于：2021年11月13日 下午
                
              </p>
            
            <div class="markdown-body">
              <h1>1. 基础语法</h1>
<h2 id="1-1-变量定义">1.1 变量定义</h2>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101112017372.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101112017372">
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101112032689.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101112032689">
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101112041353.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101112041353">
<figure class="highlight golang"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br></pre></td><td class="code"><pre><code class="hljs golang"><span class="hljs-keyword">package</span> main<br><br><span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span><br><br><span class="hljs-keyword">var</span> (<br>	aa = <span class="hljs-number">3</span><br>	ss = <span class="hljs-string">&quot;kkk&quot;</span><br>	bb = <span class="hljs-literal">true</span><br>)<br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">variableZeroValue</span><span class="hljs-params">()</span></span> &#123;<br>	<span class="hljs-keyword">var</span> a <span class="hljs-keyword">int</span><br>	<span class="hljs-keyword">var</span> s <span class="hljs-keyword">string</span><br>	fmt.Printf(<span class="hljs-string">&quot;%d %q\n&quot;</span>, a, s)<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">variableInitialValue</span><span class="hljs-params">()</span></span>  &#123;<br>	<span class="hljs-keyword">var</span> a, b <span class="hljs-keyword">int</span> = <span class="hljs-number">3</span>, <span class="hljs-number">4</span><br>	<span class="hljs-keyword">var</span> s <span class="hljs-keyword">string</span> = <span class="hljs-string">&quot;abc&quot;</span><br>	fmt.Println(a, b, s)<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">variableTypeDeduction</span><span class="hljs-params">()</span></span>  &#123;<br>	<span class="hljs-keyword">var</span> a, b, c, s = <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-literal">true</span>, <span class="hljs-string">&quot;def&quot;</span><br>	fmt.Println(a, b, c , s)<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">variableShorter</span><span class="hljs-params">()</span></span>  &#123;<br>	a, b, c, s := <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-literal">true</span>, <span class="hljs-string">&quot;def&quot;</span><br>	b = <span class="hljs-number">5</span><br>	fmt.Println(a, b, c, s)<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> &#123;<br>	fmt.Println(<span class="hljs-string">&quot;Hello World&quot;</span>)<br>	variableZeroValue()<br>	variableInitialValue()<br>	variableTypeDeduction()<br>	variableShorter()<br>&#125;<br><br></code></pre></td></tr></table></figure>
<h3 id="1-1-1-内建变量类型">1.1.1 内建变量类型</h3>
<p>bool, string</p>
<p>(u)int, (u)int8, (u)int16, (u)int32, (u)int64, uintptr(指针)</p>
<p>byte, rune(go语言的char型)</p>
<p>float32, float64, complex64, complex128(复数)</p>
<h3 id="1-1-2-强制类型转换">1.1.2 强制类型转换</h3>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101113735259.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101113735259">
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs go"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">euler</span><span class="hljs-params">()</span></span>  &#123;<br>	<span class="hljs-comment">//c := 3 + 4i</span><br>	<span class="hljs-comment">//fmt.Println(cmplx.Abs(c))</span><br>	fmt.Printf(<span class="hljs-string">&quot;%.3f\n&quot;</span>,<br>		cmplx.Pow(math.E, <span class="hljs-number">1i</span> * math.Pi) + <span class="hljs-number">1</span>)<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">triangle</span><span class="hljs-params">()</span></span>  &#123;<br>	<span class="hljs-keyword">var</span> a, b <span class="hljs-keyword">int</span> = <span class="hljs-number">3</span>, <span class="hljs-number">4</span><br>	<span class="hljs-keyword">var</span> c <span class="hljs-keyword">int</span><br>	c = <span class="hljs-keyword">int</span>(math.Sqrt(<span class="hljs-keyword">float64</span>(a * a + b * b)))<br>	fmt.Println(c)<br>&#125;<br></code></pre></td></tr></table></figure>
<h2 id="1-2-常量与枚举">1.2 常量与枚举</h2>
<h3 id="1-2-1-常量的定义">1.2.1 常量的定义</h3>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101114109703.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101114109703">
<h3 id="1-2-2-使用常量定义枚举类型">1.2.2 使用常量定义枚举类型</h3>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><code class="hljs go"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">enums</span><span class="hljs-params">()</span></span>  &#123;<br>	<span class="hljs-keyword">const</span>(<br>		cpp = <span class="hljs-literal">iota</span><br>		_<br>		python<br>		golang<br>		javascript<br>	)<br><br>	<span class="hljs-keyword">const</span> (<br>		b = <span class="hljs-number">1</span> &lt;&lt; (<span class="hljs-number">10</span> * <span class="hljs-literal">iota</span>)<br>		kb<br>		mb<br>		gb<br>		tb<br>		pb<br>	)<br>	fmt.Println(b, kb ,mb, gb, tb, pb)<br>&#125;<br></code></pre></td></tr></table></figure>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101114434629.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101114434629">
<h2 id="1-3-条件语句">1.3 条件语句</h2>
<h3 id="1-3-1-if">1.3.1 if</h3>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs go"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> &#123;<br>   <span class="hljs-keyword">const</span> filename = <span class="hljs-string">&quot;abc.txt&quot;</span><br>   <span class="hljs-keyword">if</span> contents, err := ioutil.ReadFile(filename); err != <span class="hljs-literal">nil</span> &#123;<br>      fmt.Println(err)<br>   &#125;<span class="hljs-keyword">else</span> &#123;<br>      fmt.Printf(<span class="hljs-string">&quot;%s\n&quot;</span>,contents)<br>   &#125;<br>   <br>   <span class="hljs-comment">//if err != nil &#123;</span><br>   <span class="hljs-comment">// fmt.Println(err)</span><br>   <span class="hljs-comment">//&#125; else &#123;</span><br>   <span class="hljs-comment">// fmt.Printf(&quot;%s\n&quot;,contents)</span><br>   <span class="hljs-comment">//&#125;</span><br>&#125;<br></code></pre></td></tr></table></figure>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101114923256.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101114923256">
<h3 id="1-3-2-switch">1.3.2 switch</h3>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101114944986.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101114944986">
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101115434481.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101115434481">
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs go"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">grade</span><span class="hljs-params">(score <span class="hljs-keyword">int</span>)</span> <span class="hljs-title">string</span></span> &#123;<br>	g := <span class="hljs-string">&quot;&quot;</span><br>	<span class="hljs-keyword">switch</span> &#123;<br>	<span class="hljs-keyword">case</span> score &lt; <span class="hljs-number">0</span> || score &gt; <span class="hljs-number">100</span>:<br>		<span class="hljs-built_in">panic</span>(fmt.Sprintf(<br>			<span class="hljs-string">&quot;Wrong score: %d&quot;</span>, score))<br>	<span class="hljs-keyword">case</span> score &lt; <span class="hljs-number">60</span>:<br>		g = <span class="hljs-string">&quot;F&quot;</span><br>	<span class="hljs-keyword">case</span> score &lt; <span class="hljs-number">80</span>:<br>		g = <span class="hljs-string">&quot;C&quot;</span><br>	<span class="hljs-keyword">case</span> score &lt; <span class="hljs-number">90</span>:<br>		g = <span class="hljs-string">&quot;B&quot;</span><br>	<span class="hljs-keyword">case</span> score &lt;= <span class="hljs-number">100</span>:<br>		g = <span class="hljs-string">&quot;A&quot;</span><br>	&#125;<br>	<span class="hljs-keyword">return</span> g<br>&#125;<br></code></pre></td></tr></table></figure>
<h2 id="1-4-循环（for，没有while）">1.4 循环（for，没有while）</h2>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101115527912.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101115527912">
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101120328939.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101120328939">
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101120333245.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101120333245">
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101120339280.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101120339280">
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><code class="hljs go"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">convertToBin</span><span class="hljs-params">(n <span class="hljs-keyword">int</span>)</span> <span class="hljs-title">string</span></span> &#123;<br>	result := <span class="hljs-string">&quot;&quot;</span><br>	<span class="hljs-keyword">for</span> ; n &gt; <span class="hljs-number">0</span>; n /= <span class="hljs-number">2</span> &#123;<br>		lsb := n % <span class="hljs-number">2</span><br>		result = strconv.Itoa(lsb) + result<br>	&#125;<br>	<span class="hljs-keyword">return</span> result<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">printFile</span><span class="hljs-params">(filename <span class="hljs-keyword">string</span>)</span></span>  &#123;<br>	file, err := os.Open(filename)<br>	<span class="hljs-keyword">if</span> err != <span class="hljs-literal">nil</span> &#123;<br>		<span class="hljs-built_in">panic</span>(err)<br>	&#125;<br>	scanner := bufio.NewScanner(file)<br><br>	<span class="hljs-keyword">for</span> scanner.Scan() &#123;<br>		fmt.Println(scanner.Text())<br>	&#125;<br>&#125;<br><br><span class="hljs-comment">// 死循环</span><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">forever</span><span class="hljs-params">()</span></span>  &#123;<br>	<span class="hljs-keyword">for</span> &#123;<br>		fmt.Println(<span class="hljs-string">&quot;abc&quot;</span>)<br>	&#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<h2 id="1-5-函数">1.5 函数</h2>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101121208654.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101121208654">
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101122223023.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101122223023">
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br></pre></td><td class="code"><pre><code class="hljs go"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">eval</span><span class="hljs-params">(a, b <span class="hljs-keyword">int</span>, op <span class="hljs-keyword">string</span>)</span> <span class="hljs-params">(<span class="hljs-keyword">int</span>, error)</span></span> &#123;<br>	<span class="hljs-keyword">switch</span> op &#123;<br>	<span class="hljs-keyword">case</span> <span class="hljs-string">&quot;+&quot;</span>:<br>		<span class="hljs-keyword">return</span> a + b, <span class="hljs-literal">nil</span><br>	<span class="hljs-keyword">case</span> <span class="hljs-string">&quot;-&quot;</span>:<br>		<span class="hljs-keyword">return</span> a - b, <span class="hljs-literal">nil</span><br>	<span class="hljs-keyword">case</span> <span class="hljs-string">&quot;*&quot;</span>:<br>		<span class="hljs-keyword">return</span> a * b, <span class="hljs-literal">nil</span><br>	<span class="hljs-keyword">case</span> <span class="hljs-string">&quot;/&quot;</span>:<br>		q, _ := div(a, b)<br>		<span class="hljs-keyword">return</span> q, <span class="hljs-literal">nil</span><br>	<span class="hljs-keyword">default</span>:<br>		<span class="hljs-keyword">return</span> <span class="hljs-number">0</span>, fmt.Errorf(<br>			<span class="hljs-string">&quot;unsupported operation:&quot;</span> + op)<br>	&#125;<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">div</span><span class="hljs-params">(a, b <span class="hljs-keyword">int</span>)</span> <span class="hljs-params">(q, r <span class="hljs-keyword">int</span>)</span></span> &#123;<br>	<span class="hljs-keyword">return</span> a / b, a % b<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">apply</span><span class="hljs-params">(op <span class="hljs-keyword">func</span>(<span class="hljs-keyword">int</span>, <span class="hljs-keyword">int</span>)</span> <span class="hljs-title">int</span>, <span class="hljs-title">a</span>, <span class="hljs-title">b</span> <span class="hljs-title">int</span>) <span class="hljs-title">int</span></span> &#123;<br>	p := reflect.ValueOf(op).Pointer()<br>	opName := runtime.FuncForPC(p).Name()<br>	fmt.Printf(<span class="hljs-string">&quot;Calling function %s with args&quot;</span> +<br>		<span class="hljs-string">&quot;(%d, %d)&quot;</span>, opName, a, b)<br>	<span class="hljs-keyword">return</span> op(a, b)<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">pow</span><span class="hljs-params">(a, b <span class="hljs-keyword">int</span>)</span> <span class="hljs-title">int</span></span> &#123;<br>	<span class="hljs-keyword">return</span> <span class="hljs-keyword">int</span>(math.Pow(<span class="hljs-keyword">float64</span>((a)), <span class="hljs-keyword">float64</span>(b)))<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">sum</span><span class="hljs-params">(numbers ...<span class="hljs-keyword">int</span>)</span> <span class="hljs-title">int</span></span> &#123;<br>	s := <span class="hljs-number">0</span><br>	<span class="hljs-keyword">for</span> i := <span class="hljs-keyword">range</span> numbers &#123;<br>		s += numbers[i]<br>	&#125;<br>	<span class="hljs-keyword">return</span> s<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> &#123;<br>	<span class="hljs-keyword">if</span> result, err := eval(<span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-string">&quot;x&quot;</span>); err != <span class="hljs-literal">nil</span> &#123;<br>		fmt.Println(<span class="hljs-string">&quot;Error:&quot;</span>, err)<br>	&#125; <span class="hljs-keyword">else</span> &#123;<br>		fmt.Println(result)<br>	&#125;<br>	q, r := div(<span class="hljs-number">13</span>, <span class="hljs-number">3</span>)<br>	fmt.Println(q, r)<br><br>	fmt.Println(apply(<br>		<span class="hljs-function"><span class="hljs-keyword">func</span><span class="hljs-params">(a <span class="hljs-keyword">int</span>, b <span class="hljs-keyword">int</span>)</span> <span class="hljs-title">int</span></span> &#123;<br>			<span class="hljs-keyword">return</span> <span class="hljs-keyword">int</span>(math.Pow(<br>				<span class="hljs-keyword">float64</span>(a), <span class="hljs-keyword">float64</span>(b)))<br>		&#125;, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>))<br><br>	fmt.Println(sum(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>))<br>&#125;<br></code></pre></td></tr></table></figure>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101122305379.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101122305379">
<h2 id="1-6-指针">1.6 指针</h2>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101134741974.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101134741974">
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101135043444.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101135043444">
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101135435710.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101135435710">
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101135530383.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101135530383">
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101135627873.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101135627873">
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs go"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">swap</span><span class="hljs-params">(a, b *<span class="hljs-keyword">int</span>)</span></span> &#123;<br>	*b, *a = *a, *b<br>&#125;<br><br>a, b := <span class="hljs-number">3</span>, <span class="hljs-number">4</span><br>swap(&amp;a, &amp;b)<br>fmt.Println(a, b)<br></code></pre></td></tr></table></figure>
<h1>2. 数组，切片和容器</h1>
<h2 id="2-1-数组">2.1 数组</h2>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101140327571.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101140327571">
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101140459958.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101140459958">
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101140546853.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101140546853">
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs go"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> &#123;<br>	<span class="hljs-keyword">var</span> arr1 [<span class="hljs-number">5</span>]<span class="hljs-keyword">int</span><br>	arr2 := [<span class="hljs-number">3</span>]<span class="hljs-keyword">int</span>&#123;<span class="hljs-number">1</span>, <span class="hljs-number">3</span>, <span class="hljs-number">5</span>&#125;<br>	arr3 := [...]<span class="hljs-keyword">int</span>&#123;<span class="hljs-number">2</span>, <span class="hljs-number">4</span>, <span class="hljs-number">6</span>, <span class="hljs-number">8</span>, <span class="hljs-number">10</span>&#125;<br>	<span class="hljs-keyword">var</span> gird [<span class="hljs-number">4</span>][<span class="hljs-number">5</span>]<span class="hljs-keyword">int</span><br><br>	fmt.Println(arr1, arr2, arr3)<br>	fmt.Println(gird)<br><br>	<span class="hljs-keyword">for</span> i := <span class="hljs-number">0</span>; i &lt; <span class="hljs-built_in">len</span>(arr3); i++ &#123;<br>		fmt.Println(arr3[i])<br>	&#125;<br><br>	<span class="hljs-keyword">for</span> i := <span class="hljs-keyword">range</span> arr3 &#123;<br>		fmt.Println(arr3[i])<br>	&#125;<br><br>	<span class="hljs-keyword">for</span> i, v := <span class="hljs-keyword">range</span> arr3 &#123;<br>		fmt.Println(i, v)<br>	&#125;<br><br>	<span class="hljs-keyword">for</span> _, v := <span class="hljs-keyword">range</span> arr3 &#123;<br>		fmt.Println(v)<br>	&#125;<br>&#125;<br></code></pre></td></tr></table></figure>
<h3 id="数组是值类型">数组是值类型</h3>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101141229087.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101141229087">
<p>可以使用指针引用不进行拷贝</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs go"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">printArray</span><span class="hljs-params">(arr *[5]<span class="hljs-keyword">int</span>)</span></span>  &#123;<br>	arr[<span class="hljs-number">0</span>] = <span class="hljs-number">100</span><br>	<span class="hljs-keyword">for</span> _, v := <span class="hljs-keyword">range</span> arr &#123;<br>		fmt.Println(v)<br>	&#125;<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> &#123;<br>	printArray(&amp;arr1)<br>	printArray(&amp;arr3)<br>	fmt.Println(arr1, arr3)<br>&#125;<br></code></pre></td></tr></table></figure>
<h2 id="2-2-切片">2.2 切片</h2>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101141321766.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101141321766">
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs go"><span class="hljs-comment">// 使用切片来代替上面的指针引用</span><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">printArray</span><span class="hljs-params">(arr []<span class="hljs-keyword">int</span>)</span></span>  &#123;<br>	arr[<span class="hljs-number">0</span>] = <span class="hljs-number">100</span><br>	<span class="hljs-keyword">for</span> _, v := <span class="hljs-keyword">range</span> arr &#123;<br>		fmt.Println(v)<br>	&#125;<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> &#123;<br>	printArray(arr1[:])<br>	printArray(arr3[:])<br>	fmt.Println(arr1, arr3)<br>&#125;<br></code></pre></td></tr></table></figure>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101142017550.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101142017550">
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101142236580.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101142236580">
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101142613087.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101142613087">
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101142722563.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101142722563">
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101142825322.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101142825322">
<p>ptr	指向该slice中第一个元素</p>
<p>len	表示该slice的长度</p>
<p>cap	表示底层数组的容量</p>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101142957956.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101142957956">
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs go"><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> &#123;<br>    arr := [...]<span class="hljs-keyword">int</span>&#123;<span class="hljs-number">0</span>, <span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">4</span>, <span class="hljs-number">5</span>, <span class="hljs-number">6</span>, <span class="hljs-number">7</span>&#125;<br>    <br>    fmt.Println(<span class="hljs-string">&quot;Extending slice&quot;</span>)<br>	s1 = arr[<span class="hljs-number">2</span>:<span class="hljs-number">6</span>]<br>	s2 = s1[<span class="hljs-number">3</span>:<span class="hljs-number">5</span>] <span class="hljs-comment">// [s1[3], s1[4]]</span><br>	fmt.Printf(<span class="hljs-string">&quot;s1=%v, len(s1)=%d, cap(s1)=%d\n&quot;</span>,<br>		s1, <span class="hljs-built_in">len</span>(s1), <span class="hljs-built_in">cap</span>(s1))<br>	fmt.Printf(<span class="hljs-string">&quot;s2=%v, len(s2)=%d, cap(s2)=%d\n&quot;</span>,<br>		s2, <span class="hljs-built_in">len</span>(s2), <span class="hljs-built_in">cap</span>(s2))<br>&#125;<br><br></code></pre></td></tr></table></figure>
<h3 id="1-8-1-切片操作">1.8.1 切片操作</h3>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101143212782.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101143212782">
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101143836479.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101143836479">
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br></pre></td><td class="code"><pre><code class="hljs go"><span class="hljs-keyword">package</span> main<br><br><span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span><br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">printSlice</span><span class="hljs-params">(s []<span class="hljs-keyword">int</span>)</span></span>  &#123;<br>	fmt.Printf(<span class="hljs-string">&quot;s=%v, len=%d, cap=%d\n&quot;</span>,<br>		s, <span class="hljs-built_in">len</span>(s), <span class="hljs-built_in">cap</span>(s))<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> &#123;<br>	<span class="hljs-keyword">var</span> s []<span class="hljs-keyword">int</span>	<span class="hljs-comment">// Zero value for slice is nil</span><br><br>	<span class="hljs-keyword">for</span> i := <span class="hljs-number">0</span>; i &lt; <span class="hljs-number">10</span>; i++ &#123;<br>		printSlice(s)<br>		s = <span class="hljs-built_in">append</span>(s, <span class="hljs-number">2</span> * i + <span class="hljs-number">1</span>)<br>	&#125;<br>	fmt.Println(s)<br><br>	s1 := []<span class="hljs-keyword">int</span>&#123;<span class="hljs-number">2</span>, <span class="hljs-number">4</span>, <span class="hljs-number">6</span>, <span class="hljs-number">8</span>&#125;<br>	printSlice(s1)<br><br>	s2 := <span class="hljs-built_in">make</span>([]<span class="hljs-keyword">int</span>, <span class="hljs-number">16</span>)<br>	printSlice(s2)<br><br>	s3 := <span class="hljs-built_in">make</span>([]<span class="hljs-keyword">int</span>, <span class="hljs-number">10</span>, <span class="hljs-number">32</span>) <span class="hljs-comment">// len:10 cap:32</span><br>	printSlice(s3)<br><br>	fmt.Println(<span class="hljs-string">&quot;Copying slice&quot;</span>)	<span class="hljs-comment">// 复制数组</span><br>	<span class="hljs-built_in">copy</span>(s2, s1)<br>	printSlice(s2)<br><br>	fmt.Println(<span class="hljs-string">&quot;Deleting elements from slice&quot;</span>)	<span class="hljs-comment">// 删除元素</span><br>	s2 = <span class="hljs-built_in">append</span>(s2[:<span class="hljs-number">3</span>], s2[<span class="hljs-number">4</span>:]...)<br>	printSlice(s2)<br><br>	fmt.Println(<span class="hljs-string">&quot;Popping from front&quot;</span>)	<span class="hljs-comment">// 从头部弹出</span><br>	front := s2[<span class="hljs-number">0</span>]<br>	s2 = s2[<span class="hljs-number">1</span>:]<br>	fmt.Println(front)<br>	printSlice(s2)<br><br>	fmt.Println(<span class="hljs-string">&quot;Popping from back&quot;</span>)	<span class="hljs-comment">// 从尾部弹出</span><br>	tail := s2[<span class="hljs-built_in">len</span>(s2) - <span class="hljs-number">1</span>]<br>	s2 = s2[:<span class="hljs-built_in">len</span>(s2) - <span class="hljs-number">1</span>]<br>	fmt.Println(tail)<br>	printSlice(s2)<br>&#125;<br></code></pre></td></tr></table></figure>
<h2 id="2-3-Map">2.3 Map</h2>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101145005811.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101145005811">
<p>无序 哈希表</p>
<h3 id="1-9-1-Map的操作">1.9.1 Map的操作</h3>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101145724359.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101145724359">
<h3 id="1-9-2-Map的遍历">1.9.2 Map的遍历</h3>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101145802767.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101145802767">
<h3 id="1-9-3-Map的key">1.9.3 Map的key</h3>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101145932727.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101145932727">
<h3 id="1-9-4-算法题应用-MAP（寻找最长不含有重复字符的子串）">1.9.4 算法题应用 MAP（寻找最长不含有重复字符的子串）</h3>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101150242265.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101150242265">
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101151451838.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101151451838">
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101151504407.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101151504407">
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br></pre></td><td class="code"><pre><code class="hljs go"><span class="hljs-keyword">package</span> main<br><br><span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span><br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">lengthOfNonRepeatingSubStr</span><span class="hljs-params">(s <span class="hljs-keyword">string</span>)</span> <span class="hljs-title">int</span></span> &#123;<br>	lastOccurred := <span class="hljs-built_in">make</span>(<span class="hljs-keyword">map</span>[<span class="hljs-keyword">byte</span>]<span class="hljs-keyword">int</span>)<br>	start := <span class="hljs-number">0</span><br>	maxLength := <span class="hljs-number">0</span><br><br>	<span class="hljs-keyword">for</span> i, ch := <span class="hljs-keyword">range</span> []<span class="hljs-keyword">byte</span>(s) &#123;<br>		fmt.Println(lastOccurred)<br>		<span class="hljs-keyword">if</span> lastI, ok := lastOccurred[ch]; ok &amp;&amp; lastI &gt;= start &#123;<br>			start = lastI + <span class="hljs-number">1</span><br>		&#125;<br>		<span class="hljs-keyword">if</span> i - start + <span class="hljs-number">1</span> &gt; maxLength &#123;<br>			maxLength = i - start + <span class="hljs-number">1</span><br>		&#125;<br>		lastOccurred[ch] = i<br>	&#125;<br><br>	<span class="hljs-keyword">return</span> maxLength<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> &#123;<br>	fmt.Println(<br>		lengthOfNonRepeatingSubStr(<span class="hljs-string">&quot;abcabcabc&quot;</span>))<br>	fmt.Println(<br>		lengthOfNonRepeatingSubStr(<span class="hljs-string">&quot;aaaaaaaaaa&quot;</span>))<br>	fmt.Println(<br>		lengthOfNonRepeatingSubStr(<span class="hljs-string">&quot;aqwerty&quot;</span>))<br>&#125;<br></code></pre></td></tr></table></figure>
<h2 id="2-4-字符和字符串">2.4 字符和字符串</h2>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101153326315.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101153326315">
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br></pre></td><td class="code"><pre><code class="hljs go"><span class="hljs-keyword">package</span> main<br><br><span class="hljs-keyword">import</span> (<br>	<span class="hljs-string">&quot;fmt&quot;</span><br>	<span class="hljs-string">&quot;unicode/utf8&quot;</span><br>)<br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> &#123;<br>	s := <span class="hljs-string">&quot;Yes我爱慕课网！&quot;</span> <span class="hljs-comment">// UTF-8</span><br>	fmt.Println(s)<br>	<span class="hljs-keyword">for</span> _, b := <span class="hljs-keyword">range</span> []<span class="hljs-keyword">byte</span>(s) &#123;<br>		fmt.Printf(<span class="hljs-string">&quot;%X &quot;</span>, b)<br>	&#125;<br>	fmt.Println()<br>	<span class="hljs-keyword">for</span> i, ch := <span class="hljs-keyword">range</span> s &#123; <span class="hljs-comment">// ch is a rune</span><br>		fmt.Printf(<span class="hljs-string">&quot;(%d %X) &quot;</span>, i, ch)<br>	&#125;<br>	fmt.Println()<br>	fmt.Println(<span class="hljs-string">&quot;Rune count:&quot;</span>,<br>		utf8.RuneCountInString(s))<br>	bytes := []<span class="hljs-keyword">byte</span>(s)<br>	<span class="hljs-keyword">for</span> <span class="hljs-built_in">len</span>(bytes) &gt; <span class="hljs-number">0</span> &#123;<br>		ch, size := utf8.DecodeRune(bytes)<br>		bytes = bytes[size:]<br>		fmt.Printf(<span class="hljs-string">&quot;%c &quot;</span>, ch)<br>	&#125;<br>	fmt.Println()<br>	<span class="hljs-keyword">for</span> i, ch := <span class="hljs-keyword">range</span> []<span class="hljs-keyword">rune</span>(s) &#123;<br>		fmt.Printf(<span class="hljs-string">&quot;(%d %c) &quot;</span>, i, ch)<br>	&#125;<br>	fmt.Println()<br>&#125;<br></code></pre></td></tr></table></figure>
<h3 id="2-4-1-（寻找最长不含有重复字符的子串）国际版">2.4.1 （寻找最长不含有重复字符的子串）国际版</h3>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br></pre></td><td class="code"><pre><code class="hljs go"><span class="hljs-keyword">package</span> main<br><br><span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span><br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">lengthOfNonRepeatingSubStr</span><span class="hljs-params">(s <span class="hljs-keyword">string</span>)</span> <span class="hljs-title">int</span></span> &#123;<br>	lastOccurred := <span class="hljs-built_in">make</span>(<span class="hljs-keyword">map</span>[<span class="hljs-keyword">rune</span>]<span class="hljs-keyword">int</span>) <span class="hljs-comment">// 将map[byte]int改成map[rune]int</span><br>	start := <span class="hljs-number">0</span><br>	maxLength := <span class="hljs-number">0</span><br><br>	<span class="hljs-keyword">for</span> i, ch := <span class="hljs-keyword">range</span> []<span class="hljs-keyword">rune</span>(s) &#123; <span class="hljs-comment">// 将[]byte(s)改成[]rune(s)</span><br>		<span class="hljs-keyword">if</span> lastI, ok := lastOccurred[ch]; ok &amp;&amp; lastI &gt;= start &#123;<br>			start = lastI + <span class="hljs-number">1</span><br>		&#125;<br>		<span class="hljs-keyword">if</span> i - start + <span class="hljs-number">1</span> &gt; maxLength &#123;<br>			maxLength = i - start + <span class="hljs-number">1</span><br>		&#125;<br>		lastOccurred[ch] = i<br>	&#125;<br><br>	<span class="hljs-keyword">return</span> maxLength<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> &#123;<br>	fmt.Println(<br>		lengthOfNonRepeatingSubStr(<span class="hljs-string">&quot;abcabcabc&quot;</span>))<br>	fmt.Println(<br>		lengthOfNonRepeatingSubStr(<span class="hljs-string">&quot;aaaaaaaaaa&quot;</span>))<br>	fmt.Println(<br>		lengthOfNonRepeatingSubStr(<span class="hljs-string">&quot;aqwerty&quot;</span>))<br>	fmt.Println(<br>		lengthOfNonRepeatingSubStr(<span class="hljs-string">&quot;这里是慕课网&quot;</span>))<br>	fmt.Println(<br>		lengthOfNonRepeatingSubStr(<span class="hljs-string">&quot;木木木木啊啊啊啊啊啊啊啊&quot;</span>))<br>&#125;<br></code></pre></td></tr></table></figure>
<h3 id="2-4-2-其他字符串操作">2.4.2 其他字符串操作</h3>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101153810725.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101153810725">
<h1>3 面向对象</h1>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101154132763.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101154132763">
<h2 id="3-1-结构体和方法">3.1 结构体和方法</h2>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101154142144.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101154142144">
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101154648879.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101154648879">
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br></pre></td><td class="code"><pre><code class="hljs go"><span class="hljs-keyword">package</span> main<br><br><span class="hljs-keyword">import</span> <span class="hljs-string">&quot;fmt&quot;</span><br><br><span class="hljs-keyword">type</span> treeNode <span class="hljs-keyword">struct</span> &#123;<br>	value <span class="hljs-keyword">int</span><br>	left, right *treeNode<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">createNode</span><span class="hljs-params">(value <span class="hljs-keyword">int</span>)</span> *<span class="hljs-title">treeNode</span></span> &#123;<br>	<span class="hljs-keyword">return</span> &amp;treeNode&#123;value: value&#125;<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> &#123;<br>	<span class="hljs-keyword">var</span> root treeNode<br><br>	root = treeNode&#123;value: <span class="hljs-number">3</span>&#125;<br>	root.left = &amp;treeNode&#123;&#125;<br>	root.right = &amp;treeNode&#123;<span class="hljs-number">5</span>, <span class="hljs-literal">nil</span>, <span class="hljs-literal">nil</span>&#125;<br>	root.right.left = <span class="hljs-built_in">new</span>(treeNode)<br>	root.left.right = createNode(<span class="hljs-number">2</span>)<br><br>	nodes := []treeNode &#123;<br>		&#123;value: <span class="hljs-number">3</span>&#125;,<br>		&#123;&#125;,<br>		&#123;<span class="hljs-number">6</span>, <span class="hljs-literal">nil</span>, &amp;root&#125;,<br>	&#125;<br><br>	fmt.Println(nodes)<br>&#125;<br></code></pre></td></tr></table></figure>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101154945910.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101154945910">
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs go"><span class="hljs-comment">// 第一种写法：打印节点值; 使用.print()的方法</span><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(node treeNode)</span> <span class="hljs-title">print</span><span class="hljs-params">()</span></span> &#123;<br>	fmt.Print(node.value)<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> &#123;<br>	<span class="hljs-keyword">var</span> root treeNode<br>	root = treeNode&#123;value: <span class="hljs-number">3</span>&#125;<br>	root.<span class="hljs-built_in">print</span>()<br>&#125;<br><br><span class="hljs-comment">// 第二种写法,使用print()</span><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">print</span><span class="hljs-params">(node treeNode)</span></span> &#123;<br>	fmt.Print(node.value, <span class="hljs-string">&quot; &quot;</span>)<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> &#123;<br>	<span class="hljs-keyword">var</span> root treeNode<br>	root = treeNode&#123;value: <span class="hljs-number">3</span>&#125;<br>	<span class="hljs-built_in">print</span>(root)<br>&#125;<br><br><span class="hljs-comment">// 如果修改节点值，得用指针，因为go中只有值传递</span><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(node *treeNode)</span> <span class="hljs-title">setValue</span><span class="hljs-params">(value <span class="hljs-keyword">int</span>)</span></span> &#123;<br>	node.value = value<br>&#125;<br></code></pre></td></tr></table></figure>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101160048847.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101160048847">
<h3 id="3-1-1-值接收者-vs-指针接收者">3.1.1 值接收者 vs 指针接收者</h3>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101160106479.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101160106479">
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101160836545.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101160836545">
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101161019972.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101161019972">
<h2 id="3-2-包和封装">3.2 包和封装</h2>
<h3 id="3-2-1-封装">3.2.1 封装</h3>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101161055186.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101161055186">
<h3 id="3-2-2-包">3.2.2 包</h3>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101162421752.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101162421752">
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101162443463.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101162443463">
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101170001462.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101170001462">
<ol>
<li>
<p>定义别名</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br></pre></td><td class="code"><pre><code class="hljs go"><span class="hljs-keyword">package</span> queue<br><br><span class="hljs-keyword">type</span> Queue []<span class="hljs-keyword">int</span><br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(q *Queue)</span> <span class="hljs-title">Push</span><span class="hljs-params">(v <span class="hljs-keyword">int</span>)</span></span> &#123;<br>	*q = <span class="hljs-built_in">append</span>(*q, v)<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(q *Queue)</span> <span class="hljs-title">Pop</span><span class="hljs-params">()</span> <span class="hljs-title">int</span></span> &#123;<br>	head := (*q)[<span class="hljs-number">0</span>]<br>	*q = (*q)[<span class="hljs-number">1</span>:]<br>	<span class="hljs-keyword">return</span> head<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(q *Queue)</span> <span class="hljs-title">IsEmpty</span><span class="hljs-params">()</span> <span class="hljs-title">bool</span></span> &#123;<br>	<span class="hljs-keyword">return</span> <span class="hljs-built_in">len</span>(*q) == <span class="hljs-number">0</span><br>&#125;<br></code></pre></td></tr></table></figure>
</li>
<li>
<p>使用组合</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br></pre></td><td class="code"><pre><code class="hljs go"><span class="hljs-keyword">package</span> main<br><br><span class="hljs-keyword">import</span> (<br>	<span class="hljs-string">&quot;fmt&quot;</span><br>	<span class="hljs-string">&quot;learngo/tree&quot;</span><br>)<br><span class="hljs-comment">// 使用组合</span><br><span class="hljs-keyword">type</span> myTreeNode <span class="hljs-keyword">struct</span> &#123;<br>	node *tree.Node<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(myNode *myTreeNode)</span> <span class="hljs-title">postOrder</span><span class="hljs-params">()</span></span> &#123;<br>	<span class="hljs-keyword">if</span> myNode == <span class="hljs-literal">nil</span> || myNode.node == <span class="hljs-literal">nil</span> &#123;<br>		<span class="hljs-keyword">return</span><br>	&#125;<br><br>	left := myTreeNode&#123;myNode.node.Left&#125;<br>	right := myTreeNode&#123;myNode.node.Right&#125;<br>	left.postOrder()<br>	right.postOrder()<br>	myNode.node.Print()<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> &#123;<br>	<span class="hljs-keyword">var</span> root tree.Node<br><br>	root = tree.Node&#123;Value: <span class="hljs-number">3</span>&#125;<br>	root.Left = &amp;tree.Node&#123;&#125;<br>	root.Right = &amp;tree.Node&#123;Value: <span class="hljs-number">5</span>&#125;<br>	root.Right.Left = <span class="hljs-built_in">new</span>(tree.Node)<br>	root.Left.Right = tree.CreateNode(<span class="hljs-number">2</span>)<br>	root.Right.Left.SetValue(<span class="hljs-number">4</span>)<br><br>	root.Traverse()<br>	fmt.Println()<br>	myRoot := myTreeNode&#123;&amp;root&#125;<br>	myRoot.postOrder()<br>	fmt.Println()<br>&#125;<br></code></pre></td></tr></table></figure>
</li>
<li>
<p>内嵌</p>
<figure class="highlight go"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br></pre></td><td class="code"><pre><code class="hljs go"><span class="hljs-keyword">package</span> main<br><br><span class="hljs-keyword">import</span> (<br>	<span class="hljs-string">&quot;fmt&quot;</span><br>	<span class="hljs-string">&quot;learngo/tree&quot;</span><br>)<br><br><span class="hljs-keyword">type</span> myTreeNode <span class="hljs-keyword">struct</span> &#123;<br>	*tree.Node	<span class="hljs-comment">// Embedding 内嵌</span><br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-params">(myNode *myTreeNode)</span> <span class="hljs-title">postOrder</span><span class="hljs-params">()</span></span> &#123;<br>	<span class="hljs-keyword">if</span> myNode == <span class="hljs-literal">nil</span> || myNode.Node == <span class="hljs-literal">nil</span> &#123;<br>		<span class="hljs-keyword">return</span><br>	&#125;<br><br>	left := myTreeNode&#123;myNode.Left&#125;<br>	right := myTreeNode&#123;myNode.Right&#125;<br><br>	left.postOrder()<br>	right.postOrder()<br>	myNode.Print()<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">func</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span> &#123;<br>	root := myTreeNode&#123;&amp;tree.Node&#123;Value: <span class="hljs-number">3</span>&#125;&#125;<br>	root.Left = &amp;tree.Node&#123;&#125;<br>	root.Right = &amp;tree.Node&#123;Value: <span class="hljs-number">5</span>&#125;<br>	root.Right.Left = <span class="hljs-built_in">new</span>(tree.Node)<br>	root.Left.Right = tree.CreateNode(<span class="hljs-number">2</span>)<br>	root.Right.Left.SetValue(<span class="hljs-number">4</span>)<br><br>	fmt.Print(<span class="hljs-string">&quot;In-order traversal: &quot;</span>)<br>	root.Traverse()<br>	fmt.Print(<span class="hljs-string">&quot;post-order traversal: &quot;</span>)<br>	root.postOrder()<br>	fmt.Println()<br>&#125;<br></code></pre></td></tr></table></figure>
</li>
</ol>
<h1>4. 依赖管理</h1>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101183319793.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101183319793">
<h2 id="4-1-GOPATH">4.1 GOPATH</h2>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101183458948.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101183458948">
<h2 id="4-2-GOVENDOR">4.2 GOVENDOR</h2>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101185236829.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101185236829">
<h2 id="4-3-go-mod">4.3 go mod</h2>
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101192102731.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101192102731">
<img src="/crystal575/2021/11/13/golang%E5%9F%BA%E7%A1%80%E7%9F%A5%E8%AF%86/image-20211101192322569.png" srcset="/crystal575/img/loading.gif" lazyload class="" title="image-20211101192322569">

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/crystal575/categories/golang/">golang</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/crystal575/tags/%E5%9F%BA%E7%A1%80-%E5%90%8E%E7%AB%AF/">基础 后端</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/crystal575/2021/11/13/golang%E7%BC%96%E7%A8%8B%E6%80%9D%E6%83%B3/">
                        <span class="hidden-mobile">golanggolang编程思想</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
    
  </div>
  
  <div class="statistics">
    
    

    
      
        <!-- 不蒜子统计PV -->
        <span id="busuanzi_container_site_pv" style="display: none">
            总访问量 
            <span id="busuanzi_value_site_pv"></span>
             次
          </span>
      
      
        <!-- 不蒜子统计UV -->
        <span id="busuanzi_container_site_uv" style="display: none">
            总访客数 
            <span id="busuanzi_value_site_uv"></span>
             人
          </span>
      
    
  </div>


  
  <!-- 备案信息 -->
  <div class="beian">
    <span>
      <a href="http://beian.miit.gov.cn/" target="_blank" rel="nofollow noopener">
        京ICP证123456号
      </a>
    </span>
    
      
        <span>
          <a
            href="http://www.beian.gov.cn/portal/registerSystemInfo?recordcode=12345678"
            rel="nofollow noopener"
            class="beian-police"
            target="_blank"
          >
            
              <span style="visibility: hidden; width: 0">|</span>
              <img src="/crystal575/img/police_beian.png" srcset="/crystal575/img/loading.gif" lazyload alt="police-icon"/>
            
            <span>京公网安备12345678号</span>
          </a>
        </span>
      
    
  </div>


  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.min.js" ></script>
<script  src="/crystal575/js/events.js" ></script>
<script  src="/crystal575/js/plugins.js" ></script>

<!-- Plugins -->


  
    <script  src="/crystal575/js/img-lazyload.js" ></script>
  



  



  <script  src="https://cdn.jsdelivr.net/npm/tocbot@4.12.3/dist/tocbot.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4.3.1/anchor.min.js" ></script>



  <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2.0.8/dist/clipboard.min.js" ></script>



  <script  src="/crystal575/js/local-search.js" ></script>



  <script defer src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js" ></script>




  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2.0.12/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/crystal575/js/boot.js" ></script>


</body>
</html>
